; NSIS Modern User Interface For SciTE-eg
;------------------------------------------------
; Based on :
; Basic Example Script
; Written by Joost Verburg
;------------------------------------------------
; Etienne Gaudrain <egaudrain@gmail.com> - 2008-11-25
;------------------------------------------------

;--------------------------------
;Include Modern UI

  !include "MUI2.nsh"
  !define ALL_USERS

;--------------------------------
;General

  !define PACK_NAME "SciTE-eg-2.23"
  !define PACK_VERSION "2011-01-27"

  ;Name and file
  Name "${PACK_NAME}"
  OutFile "${PACK_NAME}-${PACK_VERSION}.exe"

  ;Default installation folder
  InstallDir "$PROGRAMFILES\SciTE"
  
  ;Get installation folder from registry if available
  InstallDirRegKey HKLM "Software\${PACK_NAME}" ""

;--------------------------------
;Interface Settings

  !define MUI_ABORTWARNING

;--------------------------------
;Pages
  !define MUI_WELCOMEPAGE_TITLE "Setup of SciTE 2.23"
  !define MUI_WELCOMEPAGE_TEXT "This version of Scite has been customised for Python and R.$\r$\n$\r$\nEtienne Gaudrain <egaudrain@gmail.com> - ${PACK_VERSION}"
  !define MUI_WELCOMEFINISHPAGE_BITMAP "${NSISDIR}\Contrib\Graphics\Wizard\orange.bmp"
  
  !insertmacro MUI_PAGE_WELCOME
  !insertmacro MUI_PAGE_INSTFILES
  
  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES
  
;--------------------------------
;Languages
 
  !insertmacro MUI_LANGUAGE "English"

;--------------------------------
;Installer Sections

Section "SciTE" SecSciTE
  
  SetOutPath $INSTDIR
  File scite-bin\*
  File scite-properties\*
  File scite-icons\*.ico
  
  Rename $INSTDIR\locale.properties $INSTDIR\locale.properties.fr
  
  ; Register file types
  ; Adding Open-With-SciTE for all files
  WriteRegStr HKCR "*\shell" "" ""
  WriteRegStr HKCR "*\shell\open-with-scite" "" "Open with SciTE"
  WriteRegStr HKCR "*\shell\open-with-scite\command" "" '"$INSTDIR\scite.exe" "%1"'
  ;-- C++
  ReadRegStr $R0 HKCR ".cpp" ""
  WriteRegStr HKCR ".cpp" "" "SciTE.cpp"
  WriteRegStr HKCR "SciTE.cpp" "" "C++ Source code"
  WriteRegStr HKCR "SciTE.cpp" "OldKey" "$R0"
  WriteRegStr HKCR "SciTE.cpp\DefaultIcon" "" "$INSTDIR\cpp.ico,0"
  WriteRegStr HKCR "SciTE.cpp\shell" "" `open`
  WriteRegStr HKCR "SciTE.cpp\shell\open" "" ""
  WriteRegStr HKCR "SciTE.cpp\shell\open\command" "" '"$INSTDIR\scite.exe" "%1"'
  ;-- C
  ReadRegStr $R0 HKCR ".c" ""
  WriteRegStr HKCR ".c" "" "SciTE.c"
  WriteRegStr HKCR "SciTE.c" "" "C Source code"
  WriteRegStr HKCR "SciTE.c" "OldKey" "$R0"
  WriteRegStr HKCR "SciTE.c\DefaultIcon" "" "$INSTDIR\cpp.ico,0"
  WriteRegStr HKCR "SciTE.c\shell" "" `open`
  WriteRegStr HKCR "SciTE.c\shell\open" "" ""
  WriteRegStr HKCR "SciTE.c\shell\open\command" "" '"$INSTDIR\scite.exe" "%1"'
  ;-- H
  ReadRegStr $R0 HKCR ".h" ""
  WriteRegStr HKCR ".h" "" "SciTE.c"
  WriteRegStr HKCR "SciTE.h" "" "C/C++ Header"
  WriteRegStr HKCR "SciTE.h" "OldKey" "$R0"
  WriteRegStr HKCR "SciTE.h\DefaultIcon" "" "$INSTDIR\h.ico,0"
  WriteRegStr HKCR "SciTE.h\shell" "" `open`
  WriteRegStr HKCR "SciTE.h\shell\open" "" ""
  WriteRegStr HKCR "SciTE.h\shell\open\command" "" '"$INSTDIR\scite.exe" "%1"'
  ;-- PHP
  ReadRegStr $R0 HKCR ".php" ""
  WriteRegStr HKCR ".php" "" "SciTE.php"
  WriteRegStr HKCR "SciTE.php" "" "PHP Script"
  WriteRegStr HKCR "SciTE.php" "OldKey" "$R0"
  WriteRegStr HKCR "SciTE.php\DefaultIcon" "" "$INSTDIR\php.ico,0"
  WriteRegStr HKCR "SciTE.php\shell" "" `open`
  WriteRegStr HKCR "SciTE.php\shell\open" "" ""
  WriteRegStr HKCR "SciTE.php\shell\open\command" "" '"$INSTDIR\scite.exe" "%1"'
  ;-- CSS
  ReadRegStr $R0 HKCR ".css" ""
  WriteRegStr HKCR ".css" "" "SciTE.css"
  WriteRegStr HKCR "SciTE.css" "" "CSS Stylesheet"
  WriteRegStr HKCR "SciTE.css" "OldKey" "$R0"
  WriteRegStr HKCR "SciTE.css\DefaultIcon" "" "$INSTDIR\css.ico,0"
  WriteRegStr HKCR "SciTE.css\shell" "" `open`
  WriteRegStr HKCR "SciTE.css\shell\open" "" ""
  WriteRegStr HKCR "SciTE.css\shell\open\command" "" '"$INSTDIR\scite.exe" "%1"'
  ;-- R
  ReadRegStr $R0 HKCR ".R" ""
  WriteRegStr HKCR ".R" "" "SciTE.R"
  WriteRegStr HKCR "SciTE.R" "" "R Script"
  WriteRegStr HKCR "SciTE.R" "OldKey" "$R0"
  WriteRegStr HKCR "SciTE.R\DefaultIcon" "" "$INSTDIR\cpp.ico,0"
  WriteRegStr HKCR "SciTE.R\shell" "" `open`
  WriteRegStr HKCR "SciTE.R\shell\open" "" ""
  WriteRegStr HKCR "SciTE.R\shell\open\command" "" '"$INSTDIR\scite.exe" "%1"'
  ;-- txt
  ReadRegStr $R0 HKCR ".txt" ""
  WriteRegStr HKCR ".txt" "" "SciTE.txt"
  WriteRegStr HKCR "SciTE.txt" "" "Text file"
  WriteRegStr HKCR "SciTE.txt" "OldKey" "$R0"
  WriteRegStr HKCR "SciTE.txt\DefaultIcon" "" "$INSTDIR\txt.ico,0"
  WriteRegStr HKCR "SciTE.txt\shell" "" `open`
  WriteRegStr HKCR "SciTE.txt\shell\open" "" ""
  WriteRegStr HKCR "SciTE.txt\shell\open\command" "" '"$INSTDIR\scite.exe" "%1"'
  
  ;Shortcut
  CreateDirectory  "$SMPROGRAMS\SciTE"
  CreateShortCut "$SMPROGRAMS\SciTE\SciTE.lnk" "$INSTDIR\scite.exe" "" "" "" SW_SHOWNORMAL
  CreateShortCut "$SMPROGRAMS\SciTE\SciTE Documentation.lnk" "$INSTDIR\SciTEDoc.html" "" "" "" SW_SHOWNORMAL
  
  ;Store installation folder
  WriteRegStr HKLM "Software\${PACK_NAME}" "" "$INSTDIR"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PACK_NAME}" "DisplayName" "${PACK_NAME}"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PACK_NAME}" "UninstallString" "$INSTDIR\${PACK_NAME}-Uninstall.exe"
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PACK_NAME}" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PACK_NAME}" "NoRepair" 1
  
  ;Search for R registry key
  Var /GLOBAL Rcmd
  ClearErrors
  ReadRegStr $0 HKLM "Software\R-core\R" "InstallPath"
  IfErrors 0 +4
    MessageBox MB_OK "R was not found... You may add it manually by editing SciTE-eg.properties."
    StrCpy $Rcmd "R_command=R$\n"
  Goto +2
    StrCpy $Rcmd 'R_command="$0\bin\R.exe"$\n'
  
  ;Search for python registry key
  Var /GLOBAL Pycmd
  StrCpy $0 0
  ClearErrors
  loop:
    EnumRegKey $1 HKLM "Software\Python\PythonCore" $0
    IfErrors error_python_key +1
    StrCmp $1 "" done
    StrCpy $2 $1
    IntOp $0 $0 + 1
    Goto loop
    error_python_key:
      MessageBox MB_OK "Python was not found... You may add it manually by editing SciTE-eg.properties."
      StrCpy $Pycmd "Python_command=python$\n"
      Goto end_python_key
  done:
  ClearErrors
  ReadRegStr $0 HKLM "Software\Python\PythonCore\$2\InstallPath" ""
  IfErrors error_python_key +1
  StrCpy $Pycmd 'Python_command="$0pythonw.exe"$\n'
  end_python_key:
  
  ;Write these to the properties file
  ClearErrors
  FileOpen $0 $INSTDIR\SciTE-eg.properties w
  FileWrite $0 $Rcmd
  FileWrite $0 $Pycmd
  FileClose $0
  
  ;Create uninstaller
  WriteUninstaller "$INSTDIR\${PACK_NAME}-Uninstall.exe"
  
SectionEnd

;--------------------------------
;Uninstaller Section

; UninstallText "${APP_NAME} will be removed from the following folder. Shortcuts in the start menu will be deleted too."

Section "Uninstall"
    
    ReadRegStr $0 HKLM "Software\${PACK_NAME}" ""
    
    RMDir /r "$0"

    Delete "$0\${PACK_NAME}-Uninstall.exe"

    DeleteRegKey /ifempty HKLM "Software\${PACK_NAME}"
    DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PACK_NAME}"
    DeleteRegKey HKCR "*\shell\open-with-scite"
    DeleteRegKey /ifempty HKCR "*\shell"
    ReadRegStr $R0 HKCR "SciTE.cpp" "OldKey"
    WriteRegStr HKCR ".cpp" "" "$R0"
    DeleteRegKey HKCR "SciTE.cpp"
    ReadRegStr $R0 HKCR "SciTE.c" "OldKey"
    WriteRegStr HKCR ".c" "" "$R0"
    DeleteRegKey HKCR "SciTE.c"
    ReadRegStr $R0 HKCR "SciTE.h" "OldKey"
    WriteRegStr HKCR ".h" "" "$R0"
    DeleteRegKey HKCR "SciTE.h"
    ReadRegStr $R0 HKCR "SciTE.php" "OldKey"
    WriteRegStr HKCR ".php" "" "$R0"
    DeleteRegKey HKCR "SciTE.php"
    ReadRegStr $R0 HKCR "SciTE.css" "OldKey"
    WriteRegStr HKCR ".css" "" "$R0"
    DeleteRegKey HKCR "SciTE.css"
    ReadRegStr $R0 HKCR "SciTE.R" "OldKey"
    WriteRegStr HKCR ".R" "" "$R0"
    DeleteRegKey HKCR "SciTE.R"
    ReadRegStr $R0 HKCR "SciTE.txt" "OldKey"
    WriteRegStr HKCR ".txt" "" "$R0"
    DeleteRegKey HKCR "SciTE.txt"

SectionEnd
